package com.plwf.lake.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plwf.lake.entity.Power;
import com.plwf.lake.service.PowerService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 权限接口
* @author ：plwf
* @time：2019-5-12
*/
@RestController
@RequestMapping("/power")
public class PowerCtrl{
    @Resource
    private PowerService service;

    //修改序号
    @RequestMapping("/upxh")
    public boolean upxh(int id,int flag,int xh){
        Map<String,Integer> map = new HashMap<>();
        map.put("id", id);
        map.put("flag", flag);
        map.put("xh", xh);
        return service.saveXh(map);
    }

    //保存权限信息
    @RequestMapping("/save")
    public boolean save(Power bean){
        return service.save(bean);
    }

    //查找单个权限
    @RequestMapping("/find")
    public Power find(Integer id){
        return service.find(id);
    }

    //查询权限
    @RequestMapping("/query")
    public Map<String,Object> query(HttpServletRequest request){
        int page=1;
        int rows=5;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }
        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        List<Power> list = service.query();
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);
        return rs;
    }

    @RequestMapping("/query2")
    public List<Power> query2(){
        return service.query2();
    }

    //删除id
    @RequestMapping("/remove")
    public boolean remove(Integer id){
        return service.remove(id);
    }

    //查询所有权限
    @RequestMapping("/queryAll")
    public List<Power> queryAll(){
        return service.queryAll();
    }
}
